package com.comp7940.groupf.tgbot.config.web;

import com.comp7940.groupf.tgbot.builder.ResultBuilder;
import com.comp7940.groupf.tgbot.entity.common.Result;
import com.comp7940.groupf.tgbot.exception.BizException;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ControllerAdvice
public class ControllerExceptionHandler {
    /**
     * 自定义异常
     */
    @ExceptionHandler(Throwable.class)
    @ResponseBody
    public Result handleServiceMessageException(HttpServletRequest request, Throwable e) {
        log.error("ControllerExceptionHandler.handleServiceMessageException ",e);
        if(e instanceof BizException){
            BizException bizException = (BizException) e;
            return ResultBuilder.buildFail(501,bizException.getMessage());
        }
        return ResultBuilder.buildFail(500,"Interval Error");
    }
}